<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
use App\Model\{Cat,Article}; 

class push_list_article extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'songshu:push_list_article';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '队列模式 将已过审的文章推送到线上';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $start_time = time();
        $H = date('H');
        $H = intval($H);
        if($H>=7 && $H<=23){
            $limit = 10;
        }else{
            $limit = 5;
        }
        $articletype = Article::article_cat();
        foreach($articletype as $type=>$cat){
            $list = DB::table('check_article_queue')->where([
                ['exec','=',0],
                ['type','=',0],
                ['cat_id','=',$type]
            ])->orderBy('id','asc')->limit($limit)->get()->toArray();
            $keys = [];
            foreach($list as $row){
                $keys[$row->article_id] = $row->id;
            }
            $ids = get_array_keys($list,'article_id');
            $original = DB::connection('db2')->table('article')->whereIn('id',$ids)->where('status',1)->get();
            foreach($original as $item){
                $exists = DB::table('article')->where('title',$item->title)->first(['id','title']);
                if(empty($exists)){
                    $data = [
                        'g_id'=>0,
                        'top'=>$item->top,
                        'set_top'=>$item->set_top,
                        'title'=>$item->title,
                        'content'=>$item->content,
                        'pic'=>$item->pic,
                        'type'=>$item->type,
                        'is_hot'=>$item->is_hot,
                        'show_pv'=>$item->show_pv,
                        'pv'=>$item->pv,
                        'pv_max'=>$item->pv_max,
                        'money'=>$item->money,
                        'day'=>time(),
                        'status'=>0,
                        'downline'=>1,
                        'source'=>$item->source,
                        'type_of_article'=>$item->type_of_article,
                        'length_time'=>$item->length_time,
                        'grab'=>$item->grab,
                        'primary_url'=>$item->primary_url,
                        'ar_source'=>$item->ar_source,
                        'words_number'=>$item->words_number,
                    ];
                    $aid = DB::table('article')->insertGetId($data);
                }
                else{
                    $aid = $exists->id;
                    DB::table('check_article_queue')->where([
                        'id'=>$keys[$item->id],
                    ])->update([
                        'exec'=>1,
                        'exec_time'=>time(),
                    ]);
                }

                if($aid){
                    DB::table('check_article_queue')->where([
                        'article_id'=>$item->id,
                        'exec'=>0,
                        'type'=>0,
                    ])->update([
                        'exec'=>1,
                        'aid'=>$aid,
                        'exec_time'=>time(),
                    ]);
                    DB::connection('db2')->table('article')->where('id',$item->id)->update([
                        'status'=>0,
                        'downline'=>1,
                    ]);
                    $key = config('rediskey.article_params_zero')[$item->type];

                    Redis::zadd($key,time(),$aid);
                    echo $aid."\n";
                    echo $key."\n";
                    echo Redis::zscore($key,$aid)."\n";
                }
                
            }
        }

        $this->push_video();
        echo "\n";
        echo time()-$start_time;
    }

    public function push_video(){
        $H = date('H');
        $H = intval($H);
        if($H>=7 && $H<=23){
            $limit = 4;
        }else{
            $limit = 2;
        }
        $articletype = Article::video_cat();
        foreach($articletype as $type=>$cat){
            $list = DB::table('check_article_queue')->where([
                ['exec','=',0],
                ['type','=',1],
                ['cat_id','=',$type]
            ])->orderBy('id','asc')->limit($limit)->get()->toArray();
            $ids = get_array_keys($list,'article_id');
            $original = DB::connection('db2')->table('video')->whereIn('id',$ids)->where('status',1)->get();
            foreach($original as $item){
                $exists = DB::table('article')->where('title',$item->title)->first(['id','title']);
                if(empty($exists)){
                    $data = [
                        'g_id'=>0,
                        'top'=>$item->top,
                        'set_top'=>$item->set_top,
                        'title'=>$item->title,
                        'content'=>$item->content,
                        'pic'=>$item->pic,
                        'type'=>$item->type,
                        'is_hot'=>$item->is_hot,
                        'show_pv'=>$item->show_pv,
                        'pv'=>$item->pv,
                        'pv_max'=>$item->pv_max,
                        'money'=>$item->money,
                        'day'=>time(),
                        'status'=>0,
                        'downline'=>1,
                        'source'=>$item->source,
                        'type_of_article'=>$item->type_of_article,
                        'length_time'=>$item->length_time,
                        'grab'=>$item->grab,
                        'primary_url'=>$item->primary_url,
                        'ar_source'=>$item->ar_source,
                        'words_number'=>$item->words_number,
                    ];
                    $aid = DB::table('article')->insertGetId($data);
                }else{
                    $aid = $exists->id;
                }
                
                if($aid){
                    DB::table('check_article_queue')->where([
                        'article_id'=>$item->id,
                        'exec'=>0,
                        'type'=>1,
                    ])->update([
                        'exec'=>1,
                        'exec_time'=>time(),
                    ]);
                    DB::connection('db2')->table('video')->where('id',$item->id)->update([
                        'status'=>0,
                        'downline'=>1,
                    ]);
                    $key = config('rediskey.vod_params_zero')[$item->type];
                    Redis::zadd($key,time(),$aid);
                    echo $aid."\n";
                    echo $key."\n";
                    echo Redis::zscore($key,$aid)."\n";
                }
                
            }
        }
    }
}
